<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_filter_escape($value) {
	$pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
	$replace = '\\\$1';
	
	return preg_replace($pattern, $replace, $value);
}

//
function func_filter_phrase($value) {
	$pattern = '/("|\\\)/';
	$replace = '\\\$1';
	
	return preg_replace($pattern, $replace, $value);
}

//
function func_filter_replace($subject) {
	return str_replace(
				array('[', ']', '{', '}', '?', '$', '.', '|', '\\', '/', '\'', '"', '<', '>', '=', '_'), 
				array('\[', '\]', '\{', '\}', '\?', '\\$', '\.', '\|', '\\', '\\/', '\'', '\\"', '\\<', '\\>', '\\=', '\_'), 
				$subject);
}

//
function func_filter_post_convert($content, $attachments_ids) {
	//$content = preg_replace("/(\{\{local\}\})(\d+)(\{\{\/local\}\})/i", "{{attach}}\\2{{/attach}}", $content);
	/*
	for($i=0, $n=sizeof($attachments_ids); $i<$n; $i++) {
		if( $attachments_ids[$i] > 0 ) {
			$content = preg_replace("/(\{\{local\}\})" . ($i+1) . "(\{\{\/local\}\})/i", "{{attach}}" . $attachments_ids[$i] . "{{/attach}}", $content);
		} else {
			$content = preg_replace("/(\{\{local\}\})" . ($i+1) . "(\{\{\/local\}\})/i", "", $content);
		}
	}
	$content = preg_replace("/(\{\{local\}\})(\d+)(\{\{\/local\}\})/i", "", $content);
	*/
	for($i=0, $n=sizeof($attachments_ids); $i<$n; $i++) {
		if( $attachments_ids[$i] > 0 ) {
			$pattern[] = "/(\{\{local\}\})" . ($i+1) . "(\{\{\/local\}\})/i";
			$replacement[] = "{{attach}}" . $attachments_ids[$i] . "{{/attach}}";
		} else {
			$pattern[] = "/(\{\{local\}\})" . ($i+1) . "(\{\{\/local\}\})/i";
			$replacement[] = "";
		}
	}
	$content = preg_replace($pattern, $replacement, $content);
	$content = preg_replace("/(\{\{local\}\})(\d+)(\{\{\/local\}\})/i", "", $content);
	
	return $content;
}

function func_filter_get_attaches($content) {
	$attachments_ids = array();
	if( preg_match_all("/\{\{attach\}\}(\d+)\{\{\/attach\}\}/i", $content, $matches) ) {
		//$attachments_pattern = $matches['0'];
		$attachments_ids = $matches['1'];
	}
	
	return $attachments_ids;
}

function func_filter_attach_convert($content) {
	if( strlen($content) == 0 ) {
		return array($content, '', '');
	}
	$ids = array();
	$attrs = array();
	//<img height="401" border="0" width="400" id="attach_8" alt=" " src="../../images/resources/200812/894a25068c8c3606_vQbE6B8t.jpg" /> 
	if( preg_match_all("/\<img(.+?)\>/i", $content, $matches) ) {
		$c = 0;
		$pattern = $replacement = array();
		for($i=0, $n=sizeof($matches['1']); $i<$n; $i++) {
			$contentb = $matches['1'][$i];
			if( strpos($contentb, "attach_") !== false ) {
				$id = 0;
				$count = sizeof(preg_split("/ *=/i", trim($contentb))) - 1;
				
				$patternb = "";
				for($j=0; $j<$count; $j++) {
					$patternb .= " (.*)=\"(.*)\"";
				}
				$patternb = "/$patternb/i";
				if( preg_match_all($patternb, $contentb, $matchesb) ) {
					$c++;
					for($k=1, $m=sizeof($matchesb); $k<$m; $k=$k+2) {
						$attrs[$c][$matchesb[$k]['0']] = $matchesb[$k+1]['0'];
						if( $matchesb[$k]['0'] == 'id' ) {
							$id = substr($matchesb[$k+1]['0'], strlen('attach_'));
						}
					}
				}
				$pattern[] = "/\<img(.+?)attach\_" . $id . "(.+?)\>/";
				if( $id > 0 ) {
					$replacement[] = "{{attach}}" . $id . "{{/attach}}";
				} else {
					$replacement[] = "";
				}
			}
		}
		if( is_array($pattern) ) {
			$content = preg_replace($pattern, $replacement, $content);
		}
	}
	if( preg_match_all("/\{\{attach\}\}(\d+)\{\{\/attach\}\}/i", $content, $matches) ) {
		$ids = $matches['1'];
	}
	
	return array($content, $ids, $attrs);
}

function func_filter_convert_full($content, $isfront = true) {
	global $resObj;
	
	if( is_object($resObj) && get_class($resObj) == 'ResourceBase' ) {
		//
	} else {
		global $adb;
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.resource.class.php');
		$resObj = new CoreResourceBase();
	}
	
	//editor content replace
	//$content = preg_replace("/src\=\\\\\"(.*)images\/uploads\//", "src=\"" . CONFIG_URL_HTTP . "images/uploads/", $content);
	//$content = preg_replace("/src\=\\\\\"([\"\/\\\.])images\/uploads\//", "src=\"" . CONFIG_URL_HTTP . "images/uploads/", $content);
	//
	//$content = preg_replace("/src\=([\"\/\\\.])images\/uploads\//", "src=\"" . CONFIG_URL_HTTP . "images/uploads/", $content);
	//$content = str_replace(array('../../images/'), array(CONFIG_URL_HTTP . CONFIG_DIR_IMAGES), $content);
	//$content = str_replace(array('../images/'), array(CONFIG_URL_HTTP . CONFIG_DIR_IMAGES), $content);
	//
	$content = preg_replace("'<img(.+?)src=([\\\\])(.+?)images\/uploads\/(.+?)>'si", "<img$1src=$2\"" . CONFIG_URL_HTTP . "images/uploads/$4>", $content);
	
	//attach
	if( preg_match_all("/\{\{attach\}\}(\d+)\{\{\/attach\}\}/i", $content, $matches) ) {
		//$attachments_pattern = $matches['0'];
		$attachments_ids = $matches['1'];
		
		$attachments_idstmp = array_unique($attachments_ids);
		$attachments_arrtmp = $resObj->getItemsValues(array('ids' => $attachments_idstmp));
		if( is_array($attachments_arrtmp) ) {
			for($i=0, $n=sizeof($attachments_arrtmp); $i<$n; $i++) {
				if( strpos($attachments_arrtmp[$i]['resources_filetype'], "image") === false ) {
					if( $isfront == true ) {
						$attachments_arr[$attachments_arrtmp[$i]['resources_id']] = '<p class="attachfile" id="attach_' . $attachments_arrtmp[$i]['resources_id'] . '"><a href="' . CONFIG_URL_HTTP . 'resource/' . $attachments_arrtmp[$i]['resources_id'] . '" target="_blank">' . $attachments_arrtmp[$i]['resources_name'] . '</a></p>';
					} else {
						$attachments_arr[$attachments_arrtmp[$i]['resources_id']] = "{{attach}}" . $attachments_arrtmp[$i]['resources_id'] . "{{/attach}}";
					}
				} else {
					$attachments_arr[$attachments_arrtmp[$i]['resources_id']] = '<img class="attachimage" id="attach_' . $attachments_arrtmp[$i]['resources_id'] . '" src="' . CONFIG_URL_HTTP . CONFIG_DIR_IMAGES . 'resources/' . $attachments_arrtmp[$i]['resources_value'] . '">';
				}
			}
		}
		if( is_array($attachments_ids) ) {
			for($i=0, $n=sizeof($attachments_ids); $i<$n; $i++) {
				$attachments_pattern[] = "/(\{\{attach\}\})" . $attachments_ids[$i] . "(\{\{\/attach\}\})/";
				$attachments_replacement[] = $attachments_arr[$attachments_ids[$i]];
			}
		}
		$content = preg_replace($attachments_pattern, $attachments_replacement, $content);
	}
	
	//block
	if( $isfront == true && preg_match_all("/\{\{block\}\}(\d+)\{\{\/block\}\}/i", $content, $matches) ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/block/block.class.php');
		$blkObj = new BlockBase();
		$blkObj->format = true;
		
		//$blocks_pattern = $matches['0'];
		$blocks_ids = $matches['1'];
		
		$blocks_idstmp = array_unique($blocks_ids);
		$blocks_arrtmp = $blkObj->getItems(array('ids' => $blocks_idstmp));
		if( is_array($blocks_arrtmp) ) {
			for($i=0, $n=sizeof($blocks_arrtmp); $i<$n; $i++) {
				$blocks_arr[$blocks_arrtmp[$i]['id']] = $blocks_arrtmp[$i];
			}
		}
		
		//
		$block_template = "\n";
		$block_template .= '<div class="contBlock contBlockMC">' . "\n";
		$block_template .= '<div class="contBlockHeading %s"><span class="t">%s</span></div>' . "\n";
		$block_template .= '<div class="contBlockContent">' . "\n";
		$block_template .= '<div class="contBlockItem">' . "\n";
		$block_template .= '%s' . "\n";
		$block_template .= '</div>' . "\n";
		$block_template .= '</div>' . "\n";
		$block_template .= '</div>' . "\n";
		$block_template .= "%s";
		//
		if( is_array($blocks_ids) ) {
			for($i=0, $n=sizeof($blocks_ids); $i<$n; $i++) {
				$blocks_pattern[] = "/(\{\{block\}\})" . $blocks_ids[$i] . "(\{\{\/block\}\})/";
				
				$hidden = (($blocks_arr[$blocks_ids[$i]]['hidesubject'] == '2') ? 'hidden' : '');
				$style = '';
				if( $blocks_arr[$blocks_ids[$i]]['style'] != '' ) {
					$style .= '<style type="text/css">' . "\n" . $blocks_arr[$blocks_ids[$i]]['style'] . "</style>" . "\n";
				}
				
				$blocks_replacement[] = sprintf($block_template, $hidden, $blocks_arr[$blocks_ids[$i]]['subject'], $blocks_arr[$blocks_ids[$i]]['content'], $style);
			}
		}
		$content = preg_replace($blocks_pattern, $blocks_replacement, $content);
	}
	
	//form
	if( $isfront == true && preg_match_all("/\{\{form\}\}(\d+)\{\{\/form\}\}/i", $content, $matches) ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.filter.class.php');
		$_feObj = new FrontendFilterBase();
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.class.php');
		$formObj = new FormBase();
		$formObj->format = true;
		
		//$forms_pattern = $matches['0'];
		$forms_ids = $matches['1'];
		
		$forms_idstmp = array_unique($forms_ids);
		$forms_arrtmp = $formObj->getItemsDetail(array('ids' => $forms_idstmp));
		if( is_array($forms_arrtmp) ) {
			for($i=0, $n=sizeof($forms_arrtmp); $i<$n; $i++) {
				$forms_arr[$forms_arrtmp[$i]['id']] = $forms_arrtmp[$i];
			}
		}
		
		if( is_array($forms_ids) ) {
			for($i=0, $n=sizeof($forms_ids); $i<$n; $i++) {
				$forms_pattern[] = "/(\{\{form\}\})" . $forms_ids[$i] . "(\{\{\/form\}\})/";
				
				$_feObj->addContent('form', $forms_arr[$forms_ids[$i]]);
				$forms_replacement[] = $_feObj->fetch('common/blocks/common_block_form.tpl');
			}
		}
		$content = preg_replace($forms_pattern, $forms_replacement, $content);
	}
	
	//gallery
	if( $isfront == true && preg_match_all("/\{\{gallery\}\}(\d+)\{\{\/gallery\}\}/i", $content, $matches) ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.filter.class.php');
		$_feObj = new FrontendFilterBase();
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.class.php');
		$gaObj = new GalleryBase();
		$gaObj->format = true;
		
		//$gs_pattern = $matches['0'];
		$gs_ids = $matches['1'];
		
		$gs_idstmp = array_unique($gs_ids);
		$gs_arrtmp = $gaObj->getItemsDetail(array('ids' => $gs_idstmp));
		if( is_array($gs_arrtmp) ) {
			for($i=0, $n=sizeof($gs_arrtmp); $i<$n; $i++) {
				$gs_arr[$gs_arrtmp[$i]['album']['id']] = $gs_arrtmp[$i];
			}
		}
		
		if( is_array($gs_ids) ) {
			for($i=0, $n=sizeof($gs_ids); $i<$n; $i++) {
				$gs_pattern[] = "/(\{\{gallery\}\})" . $gs_ids[$i] . "(\{\{\/gallery\}\})/";
				
				$_feObj->addContent('album', $gs_arr[$gs_ids[$i]]['album']);
				$_feObj->addContent('items', $gs_arr[$gs_ids[$i]]['items']);
				$_feObj->addContent('item', $gs_arr[$gs_ids[$i]]['items'][0]);
				$gs_replacement[] = $_feObj->fetch('common/blocks/common_block_gallery.tpl');
			}
		}
		$content = preg_replace($gs_pattern, $gs_replacement, $content);
	}
	
	//content
	if( $isfront == true && preg_match_all("/\{\{content\}\}(\d+)\{\{\/content\}\}/i", $content, $matches) ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
		$mObj = new ContentBase();
		$mObj->format = true;
		
		//$pattern = $matches['0'];
		$ct_ids = $matches['1'];
		
		$ct_idstmp = array_unique($ct_ids);
		$ct_arrtmp = $mObj->getItems(array('ids' => $ct_idstmp));
		if( is_array($ct_arrtmp) ) {
			for($i=0, $n=sizeof($ct_arrtmp); $i<$n; $i++) {
				$ct_arr[$ct_arrtmp[$i]['id']] = $ct_arrtmp[$i];
			}
		}
		
		if( is_array($ct_ids) ) {
			for($i=0, $n=sizeof($ct_ids); $i<$n; $i++) {
				$pattern[] = "/(\{\{content\}\})" . $ct_ids[$i] . "(\{\{\/content\}\})/";
				//
				$style = '';
				if( $ct_arr[$ct_ids[$i]]['style'] != '' ) {
					$style .= '###START###<style type="text/css">' . "\n" . $ct_arr[$ct_ids[$i]]['style'] . "</style>###END###" . "\n";
				}
				//
				if( isset($ct_arr[$ct_ids[$i]]['content']) ) {
					$replacement[] = $style . $ct_arr[$ct_ids[$i]]['content'];
				} else {
					$replacement[] = $style . $ct_arr[$ct_ids[$i]]['fulltext'];
				}
			}
		}
		$content = preg_replace($pattern, $replacement, $content);
	}
	
	//static page
	if( preg_match_all("/\{\{spage id=(\d+) link=(.+?)\}\}(.+)\{\{\/spage\}\}/i", $content, $matches) ) {
		global $spObj;
		//id,link,text
		$sp_ids = $matches['1'];
		$sp_links = $matches['2'];
		$sp_texts = $matches['3'];
		
		$sp_idstmp = array_unique($sp_ids);
		$sp_arrtmp = $spObj->getItems(array('ids' => $sp_idstmp), -1, false);
		if( is_array($sp_arrtmp) ) {
			for($i=0, $n=sizeof($sp_arrtmp); $i<$n; $i++) {
				if( $sp_arrtmp[$i]['pages_status'] == 0 ) {
					$sp_arr[$sp_arrtmp[$i]['pages_id']] = '<a href="' . CONFIG_URL_HTTP . '' . $sp_arrtmp[$i]['pages_link'] . '.html">';
				} else {
					$sp_arr[$sp_arrtmp[$i]['pages_id']] = '<a href="' . CONFIG_URL_HTTP . 'static/' . $sp_arrtmp[$i]['pages_link'] . '.html">';
				}
			}
		}
		if( is_array($sp_ids) ) {
			for($i=0, $n=sizeof($sp_ids); $i<$n; $i++) {
				$sp_pattern[] = "/\{\{spage id=" . $sp_ids[$i] . " link=(.+?)\}\}" . $sp_texts[$i] . "(\{\{\/spage\}\})/";
				$sp_replacement[] = $sp_arr[$sp_ids[$i]] . $sp_texts[$i] . '</a>';
			}
		}
		$content = preg_replace($sp_pattern, $sp_replacement, $content);
	}
	
	//news page
	if( preg_match_all("/\{\{news id=(\d+) link=(.+?)\}\}(.+)\{\{\/news\}\}/i", $content, $matches) ) {
		//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/news/news.category.class.php');
		//$ncObj = new NewsCategoryBase();
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/news/news.class.php');
		$nObj = new NewsBase();
		//id,link,text
		$news_ids = $matches['1'];
		$news_links = $matches['2'];
		$news_texts = $matches['3'];
		
		$news_idstmp = array_unique($news_ids);
		$news_arrtmp = $nObj->getItems(array('ids' => $news_idstmp), -1, false);
		if( is_array($news_arrtmp) ) {
			for($i=0, $n=sizeof($news_arrtmp); $i<$n; $i++) {
				$news_arr[$news_arrtmp[$i]['pages_id']] = '<a href="' . CONFIG_URL_HTTP . 'static/' . $news_arrtmp[$i]['pages_link'] . '.html">';
			}
		}
		if( is_array($news_ids) ) {
			for($i=0, $n=sizeof($news_ids); $i<$n; $i++) {
				$news_pattern[] = "/\{\{news id=" . $news_ids[$i] . " link=(.+?)\}\}" . $news_texts[$i] . "(\{\{\/news\}\})/";
				$news_replacement[] = $news_arr[$news_ids[$i]] . $news_texts[$i] . '</a>';
			}
		}
		$content = preg_replace($news_pattern, $news_replacement, $content);
	}
	
	//front
	if( $isfront == true ) {
		//title
		if( preg_match_all("/(={2,})(.+?)(={2,})/i", $content, $matches) ) {
			$pattern = $replacement = array();
			if( is_array($matches['1']) ) {
				for($i=0, $n=sizeof($matches['1']); $i<$n; $i++) {
					$pattern[] = "/" . func_filter_replace($matches['0'][$i]) . "/";
					$replacement[] = '<h' . strlen($matches['1'][$i]) . '>' . $matches['2'][$i] . '</h' . strlen($matches['1'][$i]) . '>';
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		//link
		if( preg_match_all("/(\[{1,3})(.+?)(\]{1,3})/i", $content, $matches) ) {
			$pattern = $replacement = array();
			if( is_array($matches['1']) ) {
				for($i=0, $n=sizeof($matches['1']); $i<$n; $i++) {
					//$pattern[] = "/(\[{1,3})(.+?)(\]{1,3})/";
					$pattern[] = "/" . func_filter_replace($matches['0'][$i]) . "/";
					$t = strlen($matches['1'][$i]);
					$s = $matches['2'][$i];
					$ss = explode('|', $s);
					if( sizeof($ss) == 2 ) {
						$link = $ss['0'];
						$text = $ss['1'];
					} else {
						$link = $text = $ss['0'];
					}
					if( $t == 1 ) {
						$replacement[] = '<a href="' . $link . '">' . $text . '</a>';
					} elseif( $t == 2 ) {
						$replacement[] = '<a href="{{LINKP}}' . $link . '">' . $text . '</a>';
					} elseif( $t == 3 ) {
						$replacement[] = '<a href="' . $link . '" target="blank">' . $text . '</a>';
					}
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		//script
		$content = preg_replace("'<script[^>]*?>.*?</script>'", "", $content);
		if( preg_match_all("/\{\{\{script\}\}\}(.*?)\{\{\{\/script\}\}\}/s", $content, $matches) ) {
			$pattern = $replacement = array();
			if( is_array($matches['0']) ) {
				for($i=0, $n=sizeof($matches['0']); $i<$n; $i++) {
					$pattern[] = "/\{\{\{script\}\}\}(.*?)\{\{\{\/script\}\}\}/";
					$replacement[] = '<script type="text/javascript" language="javascript" src="$1"></script>';
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		if( preg_match_all("/\{\{script\}\}(.*?)\{\{\/script\}\}/s", $content, $matches) ) {
			$pattern = $replacement = array();
			if( is_array($matches['0']) ) {
				for($i=0, $n=sizeof($matches['0']); $i<$n; $i++) {
					$pattern[] = "/\{\{script\}\}(.*?)\{\{\/script\}\}/";
					$replacement[] = '<script type="text/javascript" language="javascript">$1</script>';
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		//style
		if( preg_match_all("/\{\{style\}\}(.*?)\{\{\/style\}\}/s", $content, $matches) ) {
			$pattern = $replacement = array();
			if( is_array($matches['0']) ) {
				for($i=0, $n=sizeof($matches['0']); $i<$n; $i++) {
					//$pattern[] = "/" . func_filter_replace($matches['0'][$i]) . "/";
					//$replacement[] = '<style type="text/css">' . str_replace("\r\n", "", $matches['1'][$i]) . '</style>';
					$pattern[] = "/\{\{style\}\}(.*?)\{\{\/style\}\}/";
					$replacement[] = '<style type="text/css">' . "\n" . '$1' . "\n" . '</style>';
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		//contactus
		if( strpos($content, "{{contactus}}") !== false ) {
			$_feObj = new FrontendBase();
			$contactus_content = $_feObj->fetch('main/common/main_common_contactus.tpl');;
			$content = str_replace("{{contactus}}", $contactus_content, $content);
		}
		//paypal
		/*
		if( strpos($content, "{{viewcart}}") !== false ) {
			_lc('payment.paypal.class.php');
			$paypalObj = new PaypalBase();
			$paypal_viewcart = $paypalObj->getCart();
			$content = str_replace("{{viewcart}}", $paypal_viewcart, $content);
		}
		*/
		//category products
		if( preg_match_all("/\{\{categoryproducts\}\}(\d+)\{\{\/categoryproducts\}\}/i", $content, $matches) ) {
			global $pObj, $pcObj;
			global $settingObj;
			$_feObj = new FrontendBase();
			
			$categories = array();
			$categories_products = array();
			$categories_ids = $matches['1'];
			$categories_idstmp = array_unique($categories_ids);
			$categories_arrtmp = $pcObj->getCategories(array('ids' => $categories_idstmp));
			for($i=0, $n=sizeof($categories_arrtmp); $i<$n; $i++) {
				$categories[$categories_arrtmp[$i]['categories_id']] = $categories_arrtmp[$i];
			}
			$products_arrtmp = $pObj->getItems(array('category' => $categories_idstmp), 0, false);
			for($i=0, $n=sizeof($products_arrtmp); $i<$n; $i++) {
				$categories_products[$products_arrtmp[$i]['categories_id']][] = $products_arrtmp[$i];
			}
			$pattern = $replacement = array();
			if( is_array($categories_idstmp) ) {
				for($i=0, $n=sizeof($categories_idstmp); $i<$n; $i++) {
					$pattern[] = "/\{\{categoryproducts\}\}" . $matches['1'][$i] . "\{\{\/categoryproducts\}\}/s";
					$_feObj->addContent('products', $categories_products[$matches['1'][$i]]);
					$_feObj->addContent('category', $categories[$matches['1'][$i]]);
					$replacement[] = $_feObj->fetch('product/common/product_common_category_products.tpl');
				}
			}
			if( is_array($pattern) ) {
				$content = preg_replace($pattern, $replacement, $content);
			}
		}
		//category navigation
		if( preg_match_all("/\{\{categorynavigation\}\}(\d+)\{\{\/categorynavigation\}\}/i", $content, $matches) ) {
			global $pObj, $pcObj;
			global $settingObj;
			$_feObj = new FrontendBase();
			
			$categories = array();
			$categories_ids = $matches['1'];
			$categories_idstmp = array_unique($categories_ids);
			for($i=0, $n=sizeof($categories_idstmp); $i<$n; $i++) {
				$pattern[] = "/\{\{categorynavigation\}\}" . $matches['1'][$i] . "\{\{\/categorynavigation\}\}/s";
				
				$category = $pcObj->getCategory($categories_idstmp[$i]);
				$categories = $pcObj->getCategoriesTree('', $categories_idstmp[$i]);
				$_feObj->addContent('category', $category);
				$_feObj->addContent('categories', $categories);
				$replacement[] = $_feObj->fetch('product/common/product_common_category_categories.tpl');
			}
			if( is_array($pattern) ) {
				$content = preg_replace($pattern, $replacement, $content);
			}
		}
		//
		$content = str_replace("{{LINKP}}", CONFIG_URL_HTTP, $content);
		//\r\n
		//
		//$content = str_replace('\r\n', "\r\n", $content);
		//$content = str_replace('\n', "\n", $content);
		//$content = str_replace('\r', "\r", $content);
		$content = str_replace(array('\r\n', '\n', '\r'), array("\r\n", "\n", "\r"), $content);
		$content = stripslashes($content);
		//
		/*
		//NOT IMPLEMENT THE FUNCTION
		if( preg_match("'<[\/\!]*?[^<>]*?>'si", $content) ) {
			$cs = explode("\n", $content);
			$ps = 0;
			for($i=0, $n=sizeof($cs); $i<$n; $i++) {
				if( strstr($cs[$i], "[[") !== false || strstr($cs[$i], "{{") !== false ) {
					$ps = 1;
				}
				if( strstr($cs[$i], "[[/") !== false || strstr($cs[$i], "{{/") !== false ) {
					$ps = 0;
				}
				if( strstr($cs[$i], "###START###") !== false ) {
					$ps = 1;
				}
				if( strstr($cs[$i], "###END###") !== false ) {
					$ps = 0;
				}
				if( $ps == 0 && strlen($cs[$i]) > 0 
										&& preg_match("'<[\/\!]*?[^<>]*?>'si", $cs[$i]) == false 
										&& preg_match("'{[\/\!]*?[^<>]*?}'si", $cs[$i]) == false 
										&& preg_match("'\=[\/\!]*?[^<>]*?\='si", $cs[$i]) == false ) {
					$cs[$i] = "<p>" . $cs[$i] . "</p>";
				}
			}
			$content = implode("\n", $cs);
		}
		//NOT IMPLEMENT THE FUNCTION
		*/
	}
	
	//
	$content = str_replace(array("###START###", "###END###"), array("", ""), $content);
	
	return $content;
}
//
?>